home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
smaltalk
/
manchest.lha
/
MANCHESTER
/
manchester
/
2.2
/
NewRandom.st
< prev
next >
Wrap
Text File
|
1993-07-24
|
4KB
|
140 lines
" NAME NewRandom
AUTHOR tph@cs.man.ac.uk
FUNCTION improved random generator
ST-VERSIONS 2.2
PREREQUISITES
CONFLICTS
DISTRIBUTION world
VERSION 1.1
DATE 22 Jan 1989
SUMMARY NewRandom
provided an alternative random number generator that
makes better use of the machine's integer arithmetic (and is not
much slower---sales plug).(2.2). TPH
"!
'From Smalltalk-80, version 2, of April 1, 1983 on 6 April 1987 at 8:51:52 pm'!
Stream subclass: #NewRandom
instanceVariableNames: 'seed a c m mm '
classVariableNames: 'ConstantA ConstantC ConstantM '
poolDictionaries: ''
category: 'Numeric-Numbers'!
!NewRandom methodsFor: 'accessing'!
contents
^self shouldNotImplement!
next
"Answer with the next random number."
| temp |
[seed _ c + (a * seed) bitAnd: mm.
0 = (temp _ seed / m)] whileTrue.
^temp!
nextPut: anObject
^self shouldNotImplement! !
!NewRandom methodsFor: 'testing'!
atEnd
^false! !
!NewRandom methodsFor: 'private'!
setSeed
"Answer a seed value from the millisecond clock, truncated to
the appropriate truncation constant. Set up instance variables
(used for performance reasons by the 'next' method.)"
a _ NewRandom constantA.
c _ NewRandom constantC.
m _ NewRandom constantM asFloat.
mm _ NewRandom constantM - 1.
seed _ Time millisecondClockValue bitAnd: mm! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
NewRandom class
instanceVariableNames: ''!
!NewRandom class methodsFor: 'instance creation'!
new
"Answer a new random number generator."
^self basicNew setSeed! !
!NewRandom class methodsFor: 'class access'!
constantA
^ConstantA!
constantC
^ConstantC!
constantM
^ConstantM! !
!NewRandom class methodsFor: 'class initialization'!
initialize
"Initialize the class variables which are the constants
in the linear congruent pseudo-random number generator.
The way to select these constants is described by D. E. Knuth
in 'The Art of Computer Programming', Vol. 2, pages 155-156."
| minA maxA |
"Truncation constant is largest SmallInteger which is also a power of 2. "
ConstantM _ 2 raisedToInteger: SmallInteger maxBits - 1.
"Additive constant is odd, and approximately the correct size."
ConstantC _ (1 / 2 - (3 sqrt / 6) * ConstantM) truncated.
ConstantC even ifTrue: [ConstantC _ ConstantC + 1].
"Multiplicative constant is between limits and a mod 8 = 5."
minA _ (ConstantM / 100) rounded max: ConstantM sqrt rounded.
maxA _ (ConstantM - ConstantM sqrt) rounded.
ConstantA _ minA + maxA // 2.
[ConstantA \\ 8 = 5]
whileFalse: [ConstantA _ ConstantA + 1]
"NewRandom initialize."! !
NewRandom initialize!
'From Smalltalk-80, version 2, of April 1, 1983 on 6 April 1987 at 8:51:42 pm'!
!SystemDictionary methodsFor: 'initialize-release'!
install
"Get connected back up to the hardware after a snapshot or quit."
"Call the initialization code that depends on system parameters,
in
case we are coming up on a system different from the one
that we
quit or snapshot on."
CompiledMethod initialize.
SmallInteger initialize.
NewRandom initialize.
LargePositiveInteger initialize.
LargeNegativeInteger initialize.
DisplayScreen currentDisplay: Display.
Cursor currentCursor: Cursor currentCursor.
InputSensor install.
LowSpaceProcess == nil ifFalse: [LowSpaceProcess terminate].
LowSpaceSemaphore _ Semaphore new.
LowSpaceProcess _ [self lowSpaceNotificationLoop] newProcess.
LowSpaceProcess priority: Processor lowIOPriority.
LowSpaceProcess resume.
self resetSpaceLimits! !
NewRandom initialize!